package com.hm.system.web;

import java.io.FileInputStream;
import java.io.OutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hm.common.annotation.AuthenticationAnnotation;
import com.hm.common.annotation.RequestPlanAnnotation;
import com.hm.common.common.CommonUtil;
import com.hm.common.model.ControllerResult;
import com.hm.common.util.CommonDef.OperationStatus;
import com.hm.system.mongo.domain.def.SysFileDef;
import com.hm.system.mongo.domain.system.SysFileInfo;
import com.hm.system.share.service.mongo.system.SysFileInfoService;
import com.hm.system.util.SystemConfigDef.Api.Rest;
import com.hm.system.web.su.BaseController;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author shishun.wang
 * @date 下午2:19:48 2016年6月8日
 * @version 1.0
 * @describe
 */
@ResponseBody
@RestController
@RequestMapping(value = Rest.Sys.REST_SYS_FILE_INFO)
public class SysFileController extends BaseController {

	public static final String swaggerTag = "系统文件管理";

	@Autowired
	private SysFileInfoService sysFileInfoService;

	@AuthenticationAnnotation
	@RequestMapping(value = "/{fileDef}", method = RequestMethod.POST)
	@ApiOperation(value = "上传文件", httpMethod = "POST", notes = "上传文件返回文件存放id", tags = swaggerTag)
	@RequestPlanAnnotation(title = "上传文件", content = "上传文件返回文件存放id")
	public ResponseEntity<ControllerResult<String>> upload(
			@ApiParam(required = true, name = "file", value = "文件对象") @RequestPart(required = true) MultipartFile file,
			@ApiParam(required = true, name = "fileDef", value = "文件类型") @PathVariable("fileDef") SysFileDef fileDef) {
		String upload = sysFileInfoService.upload(file, fileDef);
		if(CommonUtil.isNotEmpty(upload)){
			return ControllerResult.success(upload,OperationStatus.UPDATE_SUCCESSFUL);
		}else{
			return ControllerResult.failed(OperationStatus.UPLOAD_FAIL);
		}
	}

	@RequestMapping(value = "/image/{fileId}", method = RequestMethod.GET)
	@ApiOperation(value = "加载文件", httpMethod = "GET", notes = "根据文件ID加载文件", tags = swaggerTag)
	@RequestPlanAnnotation(title = "加载文件", content = "加载文件")
	public void loadFile(
			@ApiParam(required = true, name = "fileId", value = "文件编号") @PathVariable("fileId") String fileId) {
		try {
			{// 设置请求响应头
				response.setHeader("Pragma", "No-cache");
				response.setHeader("Cache-Control", "no-cache");
				response.setDateHeader("Expires", 0);
				response.setContentType("image/jpeg");
			}
			SysFileInfo sysFileInfo = sysFileInfoService.getSysFileInfoById(fileId);
			if (null != sysFileInfo) {
				FileInputStream fis = new FileInputStream(sysFileInfo.getBaseUrl());
				int size = fis.available(); // 得到文件大小
				byte data[] = new byte[size];
				fis.read(data); // 读数据
				fis.close();
				OutputStream os = response.getOutputStream();
				os.write(data);
				os.flush();
				os.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
